from datetime import datetime
from app import db

# 课程内容
class Story(db.Model):
    __tablename__ = 'story'

    Id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, comment='内容')
    type = db.Column(db.Integer, comment='类型')
    lesson_id = db.Column(db.Integer,db.ForeignKey('lesson.Id'), comment='所属课程')
    chapter_id = db.Column(db.Integer, db.ForeignKey('chapter.Id'), comment='所属课程')
    sort=db.Column(db.Integer,comment='排序')

    # 默认输出排序规则
    __mapper_args__ = {
        "order_by": sort
    }

# 课程
class Lesson(db.Model):
    __tablename__='lesson'


    Id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), comment='课程名')
    remark=db.Column(db.String(100),comment='备注')
    # 绑定章节
    chapter_list = db.relationship('Chapter', backref='chapter_lesson_info',
                                            cascade="all, delete, delete-orphan")
    # 绑定内容
    story_list = db.relationship('Story', backref='story_lesson_info',
                                   cascade="all, delete, delete-orphan")

# 课程章节
class Chapter(db.Model):
    __tablename__='chapter'


    Id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), comment='课程名')
    lesson_id = db.Column(db.Integer, db.ForeignKey('lesson.Id'), comment='所属课程')
    sort = db.Column(db.Integer, comment='排序')
    pId = db.Column(db.Integer, db.ForeignKey('chapter.Id'),comment='父Id')
    parent_chapter = db.relationship('Chapter', backref=db.backref("child_chapter"), remote_side=[Id], uselist=False)

    # 默认输出排序规则
    __mapper_args__ = {
        "order_by": sort
    }

# 练习
class Practice(db.Model):
    __tablename__='practice'

    Id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), comment='课程名')
    exercise_introduction = db.Column(db.Text, comment='练习介绍')
    lesson_id = db.Column(db.Integer, db.ForeignKey('lesson.Id'), comment='所属课程')
    subject_requirements = db.Column(db.Text, comment='题目要求')
    review_of_knowledge_points = db.Column(db.Text, comment='知识点回顾')
    type = db.Column(db.Integer, comment='类型')
    remark = db.Column(db.String(100), comment='备注')

    # 绑定练习题
    practive_lesson_info = db.relationship('Lesson', backref='practive_list',
                                    cascade="all, delete, delete-orphan",single_parent=True)

if __name__ == '__main__':
    db.create_all()